package com.apexwear.apexutils.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.RecyclerView;

import com.apexwear.apexutils.R;
import com.apexwear.apexutils.model.MultiSelect;

import java.util.List;

public class AdapterMultiDialog extends RecyclerView.Adapter<AdapterMultiDialog.MultiHold> {
    List<MultiSelect> multiSelectList;
    Context mContext;
    final String TAG = this.getClass().getSimpleName();

    public AdapterMultiDialog(List<MultiSelect> multiSelectList, Context mContext) {
        this.multiSelectList = multiSelectList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public MultiHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View adapterView = LayoutInflater.from(mContext).inflate(R.layout.adapter_dialog_cycle_list, parent, false);
        return new MultiHold(adapterView);
    }

    @Override
    public void onBindViewHolder(@NonNull MultiHold holder, int position) {
        if (multiSelectList == null || multiSelectList.isEmpty()) {
            return;
        }
        holder.cycleTxt.setText(multiSelectList.get(position).getTxt());
        if (multiSelectList.get(position).isSelect()) {
            holder.checkBox.setImageResource(R.mipmap.check_select);
        } else {
            holder.checkBox.setImageResource(R.mipmap.check_rect);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (multiSelectList.get(position).isSelect()) {
                    multiSelectList.get(position).setSelect(false);
                    holder.checkBox.setImageResource(R.mipmap.check_rect);

                } else {
                    multiSelectList.get(position).setSelect(true);
                    holder.checkBox.setImageResource(R.mipmap.check_select);

                    if (multiSelectList.get(position).getTxt().equalsIgnoreCase(mContext.getString(R.string.cycle_8))) {
                        for (int i = 0; i < multiSelectList.size(); i++) {
                            if (multiSelectList.get(i).isSelect() && !multiSelectList.get(i).getTxt().equalsIgnoreCase(mContext.getString(R.string.cycle_8))) {
                                multiSelectList.get(i).setSelect(false);
                                notifyItemChanged(i, TAG);
                            }
                        }
                    } else {
                        for (int i = 0; i < multiSelectList.size(); i++) {
                            if (multiSelectList.get(i).isSelect() && multiSelectList.get(i).getTxt().equalsIgnoreCase(mContext.getString(R.string.cycle_8))) {
                                multiSelectList.get(i).setSelect(false);
                                notifyItemChanged(i, TAG);
                                break;
                            }
                        }
                    }

                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return multiSelectList == null ? 0 : multiSelectList.size();
    }

    class MultiHold extends RecyclerView.ViewHolder {
        AppCompatImageView checkBox;
        TextView cycleTxt;

        public MultiHold(@NonNull View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.check);
            cycleTxt = itemView.findViewById(R.id.cycle_txt);

        }
    }

    public void destroyAdapter() {
        mContext = null;
        if (multiSelectList != null) {
            multiSelectList.clear();
            multiSelectList = null;
        }
    }
}
